package com.aspire.moss.portal.desktop;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Process;
import android.util.Log;

import com.aspire.moss.portal.R;

public class Main extends Activity {

	private static final String TAG = "Main";

	private Context mContext;

	private Workspace workspace;

	ClipImageView clipImageView;
	
	EventCenter eventCenter;
	
	DragLayer dragLayer;
	
	WorkspaceAdapter adapter ;
	
	DataAdapter bottomDataAdapter;
	
	Dock dock;

	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "Main : onCreate");
		mContext = this;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.d(TAG, "density :" + mContext.getResources().getDisplayMetrics().densityDpi);
		dragLayer = (DragLayer) findViewById(R.id.draglayer);
		workspace = (Workspace) findViewById(R.id.workspace);
		clipImageView = (ClipImageView) findViewById(R.id.clipview);
		dock = (Dock) findViewById(R.id.bottomdesktop);
		workspace.setWorkspaceListener(clipImageView);
		eventCenter = new EventCenter(mContext, workspace ,dragLayer );
		dragLayer.setInterceptTouchEvent(eventCenter);
		HashMap<Integer,LinkedList<AppInfo>> apps = createDatasourre();
		adapter = new WorkspaceAdapter(mContext, apps);
		adapter.setOnClickListener(eventCenter);
		adapter.setOnLongClickListener(eventCenter);
		adapter.setOnDeleteClickListener(eventCenter);
		
		bottomDataAdapter = new DataAdapter(mContext, createBottomApp());
		bottomDataAdapter.setOnClickListener(eventCenter);
		bottomDataAdapter.setOnDeleteClickListener(eventCenter);
		bottomDataAdapter.setOnLongClickListener(eventCenter);
		dock.setAdapter(bottomDataAdapter);
		bottomDataAdapter.notifyDataSetChanged();
		
		workspace.setWorkspaceAdapter(adapter);
		adapter.notifyObservers();
		
//		getSharedPreferences(name, mode)
	}
	
	private HashMap<Integer,LinkedList<AppInfo>> createDatasourre(){
		
		HashMap<Integer,LinkedList<AppInfo>> apps =new HashMap<Integer,LinkedList<AppInfo>>();
		LinkedList<AppInfo> page = null;
		Random random = new Random();
		AppInfo info = null;
		for(int i = 0 ;i < 4 ;i++){
			page = new LinkedList<AppInfo>();
			info = new AppInfo(i, 0, 0, getBitmap() , "程序程序" + random.nextInt(10), "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 1, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 2, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 3, getBitmap(), "程序程" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 0, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 1, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 2, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 3, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 0, getBitmap() , "程序程" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 1, getBitmap() , "程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 2, getBitmap() , "程序程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 3, getBitmap() , "程序程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 0, getBitmap() , "程序" + random.nextInt(10), "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 1, getBitmap() , "程序" + random.nextInt(10), "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 2, getBitmap() , "程序程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 3, getBitmap() , "程序程序" + random.nextInt(10) , "程序程序" + random.nextInt(10));
			page.add(info);
			apps.put(i, page);
		}
		return apps;
	}
	
	public LinkedList<AppInfo> createBottomApp(){
		LinkedList<AppInfo> page = new LinkedList<AppInfo>();
		Random random = new Random();
		AppInfo info = null;
		for(int i = 0 ;i < 4 ;i++){
			info = new AppInfo(i, 0, 0, getBitmap() , "程序程序" + random.nextInt(10), "程序程序" + random.nextInt(10));
			page.add(info);
		}
		return page;
	}
	
	private Bitmap getBitmap(){
		int id [] = {
				R.drawable.browser,
		        R.drawable.cal,
		        R.drawable.calc,
		        R.drawable.chat_blank,
		        R.drawable.clock,
		        R.drawable.graph,
		        R.drawable.ipod,
		        R.drawable.mail,
		        R.drawable.map,
		        R.drawable.notes,
		        R.drawable.phone,
		        R.drawable.photo,
		        R.drawable.sms,
		        R.drawable.tools,
		        R.drawable.wallpaper,
		        R.drawable.weather
		};
		Random random = new Random();
		int idIndex = random.nextInt(id.length);
		Drawable  drawable = getResources().getDrawable(id[idIndex]);
		return ((BitmapDrawable)drawable ).getBitmap();
	}

//	private Bitmap excuteBitmap(Drawable drawable) {
//		Drawable temp = drawable;
//		while(temp==null){
//			temp = getBitmap();
//		}
//		BitmapDrawable bitmapDrawable =(BitmapDrawable) temp;
//		Bitmap bitmap = bitmapDrawable.getBitmap();
//		
//		int icon = (int) mContext.getResources().getDimension(R.dimen.icon);
//		Bitmap app = Bitmap.createBitmap(icon, icon, Config.ARGB_8888);
//		
//		android.graphics.Matrix matrix = new Matrix();
//		matrix.preScale(icon, icon);
//		Canvas canvas = new Canvas(app);
//		RectF rect = new RectF(0, 0, icon, icon);
//		Paint paint = new Paint();
//		paint.setFlags(Paint.FILTER_BITMAP_FLAG);
//		canvas.drawRect(rect , paint );
//		canvas.drawBitmap(bitmap, matrix, null);
//		canvas.save();
//		return app;
//	}
	
	@Override
	public void onBackPressed() {
		if(workspace.isLockWorkspace()){
			workspace.unlockWorkspace();
		}else{
			super.onBackPressed();
			Process.killProcess(Process.myPid());
		}
		
		adapter.printInfo();
	}
	
}
